//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.services
{
	import __AS3__.vec.Vector;
	
	import com.pbking.facebook.Facebook;
	import com.pbking.facebook.data.photos.FacebookAlbum;
	import com.pbking.facebook.data.photos.FacebookPhoto;
	import com.pbking.facebook.delegates.photos.GetAlbumsDelegate;
	import com.pbking.facebook.delegates.photos.GetPhotosDelegate;
	import com.pbking.facebook.delegates.photos.UploadDelegate;
	import com.pbking.facebook.events.FacebookActionEvent;
	
	import flash.events.Event;
	
	import mx.core.UIComponent;
	
	import reader.ApplicationFacade;
	import reader.model.IDraftProxy;
	import reader.model.IServiceProxy;
	import reader.model.vo.AlbumPhotosVO;
	import reader.model.vo.AlbumVO;
	import reader.model.vo.BlogDraftVO;
	import reader.model.vo.MediaVO;
	import reader.model.vo.PhotoVO;
	import reader.model.vo.ServiceVO;
	import reader.model.vo.UploadPhotoVO;
	import reader.utils.CryptoUtil;

	public class FacebookReaderService extends AbstractService 
										implements IGetAlbumAble, IUploadPhotoAble/* , IPostBlogAble */
	{
		////////////////////////////////////////////////////////////////////////////////
		//private variables
		
		private var _isGetAlbumAble:Boolean;
		private var _isUploadPhotoAble:Boolean;
		private var _isPostBlogAble:Boolean;
		
		private var _isFacebookConnected:Boolean;
		
		private var _service:Facebook;
		private var _draftProxy:IDraftProxy;
		private var _facebookAuthRequest:FacebookAuthRequest;
		
		//////////////////////////////////////////////////////////////////////////////
		//constructor
		public function FacebookReaderService(service:ServiceVO, proxy:IServiceProxy, parent:UIComponent=null)
		{
			super(service, proxy, parent);
			this.initialize();
		}
		////////////////////////////////////////////////////////////////////////////////
		//methods
		
		//----------------------------------------------------------------------------
		//
		// service initialize
		//
		//----------------------------------------------------------------------------
		
		override protected function initialize():void
		{
			_service = new Facebook();
			_facebookAuthRequest = new FacebookAuthRequest(_service, service, proxy);
			_facebookAuthRequest.request(onFacebookInit, CryptoUtil.decrypt(service.secret, SERVICE_SECRET));
		}
		
		private function onFacebookInit():void
		{
			_isFacebookConnected = true;
		}
		
		//----------------------------------------------------------------------------
		//
		// get albums
		//
		//----------------------------------------------------------------------------

		public function get isGetAlbumAble():Boolean
		{
			return _isGetAlbumAble;
		}
		
		public function set isGetAlbumAble(value:Boolean):void
		{
			_isGetAlbumAble = value;
		}
		
		public function getAlbums():void
		{
			if(!_service)
			{
				initialize();
				return;
			}
			_service.photos.getAlbums(_service.user.uid, true, onGetMyAlbumReady_handler);
		}
		private function onGetMyAlbumReady_handler(e:Event):void
		{
			var albums:GetAlbumsDelegate = e.target as GetAlbumsDelegate;
			var list:Vector.<AlbumVO> = new Vector.<AlbumVO>();
			for(var i:int = 0; i < albums.albums.length; i++)
			{
				var av:AlbumVO = new AlbumVO();
				var ab:FacebookAlbum = albums.albums[i];
				av.albumid = ab.aid;
				av.albumuri = ab.link;
				av.defaultimage = ab.cover.src_small;
				av.title = ab.name;
				av.photosCount = ab.size;
				list.push(av);
			}
			proxy.sendNotification(ApplicationFacade.GET_ALBUMS, list, service.name);
			
		}
		
		public function getAlbumPhotos(param:AlbumVO):void
		{
			_service.photos.getPhotos(null, int(param.albumid), null, onGetAlbumPhotos_handler);
		}
		private function onGetAlbumPhotos_handler(e:Event):void
		{
			var albums:GetPhotosDelegate = e.target as GetPhotosDelegate;
			var ap:AlbumPhotosVO = new AlbumPhotosVO();
			for(var i:int = 0; i < albums.photos.length; i++)
			{
				var av:PhotoVO = new PhotoVO();
				var ph:FacebookPhoto = albums.photos[i];
				av.caption = ph.caption;
				av.imageuri = ph.src_big;
				av.photoid = ph.pid;
				av.smallimageuri = ph.src_small;
				ap.list.push(av);
			}
			ap.defaultimage = ap.list[0].src_small;
			ap.title = ap.list[0].caption;
			ap.photosCount = ap.list.length;
			proxy.sendNotification(ApplicationFacade.GET_ALBUM_PHOTOS, ap, service.name);
		}
		
		
		
		//----------------------------------------------------------------------------
		//
		// upload photo
		//
		//----------------------------------------------------------------------------
		
		public function get iDraftProxy():IDraftProxy
		{
			return _draftProxy;
		}
		
		public function set iDraftProxy(proxy:IDraftProxy):void
		{
			_draftProxy = proxy;
		}
		
		public function get isUploadPhotoAble():Boolean
		{
			return _isUploadPhotoAble;
		}
		
		public function set isUploadPhotoAble(value:Boolean):void
		{
			_isUploadPhotoAble = value;
		}
		
		private var filename:String;
		public function uploadPhotos(file:UploadPhotoVO, album:AlbumVO = null):void
		{
			filename = file.fileName;
			_service.photos.upload(file.image, new FacebookAlbum({aid: album.albumid}), file.extension, onUploadResponse_handler);
		}
		private function onUploadResponse_handler(e:Event):void
		{
			var ph:UploadDelegate = e.target as UploadDelegate;
			var av:PhotoVO = new PhotoVO();
			if(ph && ph.uploadedPhoto)
			{
				av.caption = ph.uploadedPhoto.caption;
				av.imageuri = ph.uploadedPhoto.src_big;
				av.photoid = ph.uploadedPhoto.pid;
				av.smallimageuri = ph.uploadedPhoto.src_small;
			}
			var media:MediaVO = new MediaVO(av);
			media.name = filename;
			media.platform = service.name;
			filename = null;
			iDraftProxy.saveMedia(media);
			proxy.sendNotification(ApplicationFacade.UPLOAD_PHOTO, av, service.name);
		}
		
		//----------------------------------------------------------------------------
		//
		// post blog
		//
		//----------------------------------------------------------------------------
		
		/* public function get isPostBlogAble():Boolean
		{
			return _isPostBlogAble;
		}
		
		public function set isPostBlogAble(value:Boolean):void
		{
			_isPostBlogAble = value;
		}
		
		public function newBlog(blog:BlogDraftVO):void
		{
			if(!_service)
				initialize();
			
		} */
		
	}
}

import com.pbking.facebook.Facebook;
import com.pbking.facebook.events.FacebookActionEvent;

import reader.model.vo.ServiceVO;
import reader.model.IServiceProxy;
import reader.utils.URL;

import flash.events.Event;
import flash.events.IOErrorEvent;

class FacebookAuthRequest
{
	////////////////////////////////////////////////////////////////////////////////
	//private variables
	
	private var facebook:Facebook;
	private var service:ServiceVO;
	private var callback:Function;
	private var proxy:IServiceProxy;
	
	private var authperm:String;
	
	//////////////////////////////////////////////////////////////////////////////
	//constructor
	public function FacebookAuthRequest(facebook:Facebook, service:ServiceVO, proxy:IServiceProxy)
	{
		this.facebook = facebook;
		this.service = service;
		this.proxy = proxy;
	}
	
	////////////////////////////////////////////////////////////////////////////////
	//methods
		
	public function request(callback:Function, secret:String):void
	{
		this.callback = callback;
		facebook.addEventListener(Event.COMPLETE, onFacebookReady_handler);
		facebook.addEventListener(FacebookActionEvent.WAITING_FOR_LOGIN, onWaitingForLogin_handler);
		facebook.startDesktopSession(service.key, secret);
	}
	
	private function onWaitingForLogin_handler(e:FacebookActionEvent):void
	{
		facebook.removeEventListener(FacebookActionEvent.WAITING_FOR_LOGIN, onWaitingForLogin_handler);
		service.login_url = facebook.login_url + "?api_key=" + service.key + "&v=1.0&auth_token=" + e.authToken;
		proxy.requestAuthWindow(service, authCallBack);
	}
	
	private function authCallBack(url:URL):void
	{
		facebook.validateDesktopSession();	
	}
	
	private function onFacebookReady_handler(event:Event):void
	{
		facebook.removeEventListener(Event.COMPLETE, onFacebookReady_handler);
		if(facebook.isConnected)
		{
			callback();
		}
		else
		{
			facebook.dispatchEvent(new Event(IOErrorEvent.IO_ERROR));
		}
		
	}			
	
}